IF OBJECT_ID('ssf.ptx_generate_task_job_name') IS NOT NULL
	DROP FUNCTION ssf.ptx_generate_task_job_name
GO

CREATE FUNCTION ssf.ptx_generate_task_job_name (
	@task_group_id INT
	, @task_id INT
	, @run_date DATETIME
)
RETURNS NVARCHAR(128)
AS
BEGIN
/*
<object_header>

<object_summary>
	<purpose><![CDATA[
Generate a task job name.
Name will start with "xt_" - meaning "execute task",
starting with "xt_" will keep all the dynamic jobs grouped together near the bottom of the job list.
	]]></purpose>
</object_summary>

</object_header>
*/

	RETURN (
		N'xt'
		+ N'_' + CAST(@task_group_id AS NVARCHAR(10))
		+ N'_' + CAST(@task_id AS NVARCHAR(10))
		+ N'_' + REPLACE(REPLACE(REPLACE(CONVERT(NVARCHAR(20), ISNULL(@run_date, GETDATE()) ,120), N'-', N''), N':', N''), NCHAR(32), N'')
		+ N'_' + CAST(@@SPID AS NVARCHAR(10))
	)
END
GO
